from django.contrib import admin
from django.contrib.admin import ModelAdmin

from goods import models


class GoodsCategoryAdmin(ModelAdmin):
    # 管理后台监听数据的改变
    def save_model(self, request, obj, form, change):
        """admin后台新增或修改了数据时调用"""
        obj.save()
        from celery_tasks.html.tasks import generate_static_list_search_html
        # generate_static_list_search_html()
        generate_static_list_search_html.delay()   # 调用celery的delay方法解决耗时问题 并重新生成半静态的页面

    def delete_model(self, request, obj):
        """admin后台删除了数据时调用"""
        obj.delete()
        from celery_tasks.html.tasks import generate_static_list_search_html
        # generate_static_list_search_html()  # 重新生成半静态化页面
        generate_static_list_search_html.delay()  # 重新生成半静态化页面 在新增数据和修改数据的时候不会发生阻塞 异步生成静态页面


admin.site.register(models.GoodsCategory, GoodsCategoryAdmin)
admin.site.register(models.GoodsChannel)
admin.site.register(models.Brand)
admin.site.register(models.Goods)
admin.site.register(models.GoodsSpecification)
admin.site.register(models.SpecificationOption)
admin.site.register(models.SKU)
admin.site.register(models.SKUImage)
admin.site.register(models.SKUSpecification)